<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category extends Modules_Controller
{

	function __construct()
	{
		parent::__construct();

		$this->rules = array(
			"rule" => array(
				array(
					"field" => "title",
					"label" => lang('title'),
					"rules" => "trim|required|min_length[1]"
				)
			)
		);

	}
	public function index(){
		// 栏目路径
		if(isset($_GET['p1'])){$page=$_GET['p1'];}else{$page=1;}
		$vdata['cpath']= $this->mcol->get_path_more($this->cid);
		$vdata['cchildren'] = $this->mcol->get_cols($this->cid);
		$title = $this->mcol->get_one($this->cid,"title");
		$vdata['title'] = $title['title'];

		$limit = $this->page_limit;
		$this->input->get('limit',TRUE) and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');

		$order = $this->_index_orders();
		if ($this->input->get('order',TRUE)) {
			// TODO: order
			// $orders = explode("-",$this->input->get('order',TRUE));
			$order = $this->input->get('order',TRUE);
		}
		$where_in = array("category.cid"=>$this->cid,"category.ccid"=>$this->ccid);
		// 条件必须
		$fields = $this->_list_fields();
		$where = array_merge($this->_index_where(),$where_in);
		$vdata['pages'] = $this->_pages(site_url($this->class.'/index/'.$this->cid.'/'),$limit,$where,4);
		$vdata['list'] = $this->model->get_list($limit,$limit*($page-1),$order,$where,$fields,false,array('coltypes','coltypes.id = category.ctype','left'));
		$this->_display($vdata);
	}
	// 搜索
	public function search($cid=false,$page=1){
		$vdata['cpath']= $this->mcol->get_path_more($this->cid);
		$vdata['cchildren'] = $this->mcol->get_cols($this->cid);
		$title = $this->mcol->get_one($this->cid,"title");
		$vdata['title'] = $title['title'];
		$limit = $this->page_limit;
		$this->input->get('limit',TRUE) and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');
		$order = $this->input->get('order',TRUE) ? $this->input->get('order',TRUE):FALSE;
		$where = array();
		$where['category.cid'] = $this->cid;
		if ($ctype = $this->input->get('ctype',TRUE) AND is_numeric($ctype)) {
			$ctypes_ids = $this->mctypes->find_ids($this->cid,$ctype);
			$where['in'] = array('category.ctype',$ctypes_ids);
		}
		if ($wh = $this->_search_where()) {
			$where = array_merge($where,$wh);
		}
		if($this->input->get('q',TRUE)){
			$where_q = array(
				'like category.title'=> array('category.title',$this->input->get('q',TRUE))
			);
			$where = array_merge($where,$where_q);
		}
		$fields = $this->_list_fields();
		$vdata['pages'] = $this->_pages(site_url($this->class.'/search/'.$this->cid.'/'),$limit,$where,4);
		$vdata['list'] = $this->model->get_list($limit,$limit*($page-1),$order,$where,$fields,false,array('coltypes','coltypes.id = category.ctype','left'));
		$this->_display($vdata,$this->class.'_index.php');
	}
	protected function _list_fields(){
		return 'category.*,coltypes.title as ctitle';
	}
	public function copypro()
    {
        $ids = $this->input->post('ids');

        $rs=$this->model->get_one($ids);

        unset($rs['id']);
        unset($rs['sort_id']);
        unset($rs['timeline']);

        $id = $this->model->create($rs);
        if ($id) {
            $vdata['msg'] = '复制成功，请刷新查看';
            $vdata['status'] = 1;
        }else{
            $vdata['msg'] = '复制失败，请刷新后重试';
            $vdata['status'] = 0;
        }
        $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
    }
	protected function _create_data(){
		$form=$this->input->post();
		$form['timeline'] = strtotime($form['timeline']);
		return $form;
	}
	protected function _edit_data(){
		$form=$this->input->post();
		$form['timeline'] = strtotime($form['timeline']);
		return $form;
	}

}
